package com.higgins.eventtracking.client.health.fundamentals;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.TabBar;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.health.components.GenericChartPanel;
import com.higgins.eventtracking.client.health.components.GenericStatsTabParent;
import com.higgins.eventtracking.client.health.components.GenericTabInterface;

public class NegativeTab extends VerticalPanel implements GenericTabInterface, GenericStatsTabParent		{
	private boolean firstRun = true;
	private TabPanel tabPanel = new TabPanel();
	private Fundamentals parentTab = null;
	
	private String anxiety = "Anxious";
	private String depression = "Depressed";
	private String bored = "Bored";
	
	private Integer tab1 = 0;
	private Integer tab2 = 0;
	private Integer tab3 = 0;
		
	private NegativeSubTab anxietyTab = new NegativeSubTab(this, anxiety);
	private NegativeSubTab depressionTab = new NegativeSubTab(this, depression);
	private NegativeSubTab boredTab = new NegativeSubTab(this, bored);

	private String tabTitle = null;
	private Integer tabIndex = null;
	
	public NegativeTab(Fundamentals parentTab)	{
		this.parentTab = parentTab;
		setWidth("800px");

	}
	public void update()	{
		if (firstRun)	
			initialise();
	}
	public void initialise()	{
		populateTabPanel();
		firstRun = false;
	}
	public void populateTabPanel()	{
		anxietyTab.update();
		depressionTab.update();
		boredTab.update();
		addTabs();
		add(tabPanel);
		tabPanel.addSelectionHandler(createSelectionHandler());
	}
	public SelectionHandler<Integer> createSelectionHandler()	{
		SelectionHandler<Integer> selectionHandler = new SelectionHandler<Integer>() {
	        @Override
	        public void onSelection(SelectionEvent<Integer> integerSelectionEvent) {
	            if (integerSelectionEvent.getSelectedItem() == 0)	{
	            	anxietyTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 1)	{
	            	depressionTab.update();
	            }
	            if (integerSelectionEvent.getSelectedItem() == 2)	{
	            	boredTab.update();
	            }
	        }
		};
		return selectionHandler;
	}
	public void setSubHeaderCount(String header, int count)	{
		if (header.equals(anxiety))	{
			tab1 = count;
		} else if (header.equals(depression))	{
			tab2 = count;
		} else if (header.equals(bored))	{
			tab3 = count;
		} 
		int total = tab1 + tab2 + tab3;
		parentTab.updateTabTitle(tabIndex, tabTitle + "(" + total  + ")", false);
//		passBackToParent();
	}
//	private void passBackToParent()	{
//		if (tab1!=null && tab2!=null && tab3!=null)	{
//			addTabs();
//			parentTab.addNegativeTab(tab1+tab2+tab3);
//		}
//	}
	private void addTabs()	{
		int subTabIndex = 0;
		anxietyTab.setTabIndex(subTabIndex++);
		tabPanel.add(anxietyTab, anxiety);
		tabPanel.selectTab(0);
		depressionTab.setTabIndex(subTabIndex++);
		tabPanel.add(depressionTab, depression);
		boredTab.setTabIndex(subTabIndex++);
		tabPanel.add(boredTab, bored );
		tabPanel.selectTab(0);
	}
	
	@Override
	public String getTabTitle() {
		return tabTitle;
	}

	@Override
	public void setTabTitle(String tabTitle) {
		this.tabTitle = tabTitle;
	}

	@Override
	public Integer getTabIndex() {
		return tabIndex;
	}

	@Override
	public void setTabIndex(Integer tabIndex) {
		this.tabIndex = tabIndex;
	}
	@Override
	public void updateTabTitle(Integer tabIndex, String title, Boolean alert) {
		TabBar tabBar = tabPanel.getTabBar();
		tabBar.setTabHTML(tabIndex, title);
	}
	@Override
	public void addChartPanel(GenericChartPanel chartPanel) {
		// TODO Auto-generated method stub
		
	}
	
}
